/*******************************************************************************
																				
	DESCRIPTION:  	This do file generates panel C of Table 3 and Appendix Table A6.
	
*******************************************************************************/

clear all
global id_code 111_3
pause on
set seed 2110

* Set year:
local year 2006

/*******************************************************************************
*	Predicted job-finding rates - time series - predictions done by year
********************************************************************************/

* Create frame for results:
frame create sum str1000(vars) R_sq beta cov

foreach model in  ///
			/// Extended models (Full + other data):
			Full EX_FullSample_OC EX_FullSample_UM EX_FullSample_UI  ///
			EX_FullSample_RR EX_FullSample_WE EX_FullSample_IQ  EX_FullSample_ALL ///
			/// Extended models (Basic + other data):
			Full_SeqDrop_incIndiv EX_Ba_FullSample_OC EX_Ba_FullSample_UM EX_Ba_FullSample_UI  ///
			EX_Ba_FullSample_RR EX_Ba_FullSample_WE EX_Ba_FullSample_IQ  EX_Ba_FullSample_ALL {
			

	 * Load the data
	use "${data}/003_MainWithEnsemblePred_`model'_`year'.dta", clear


	* Calculate the beta and the R-squared from regressing the empirical JFR on the predicted JFR
	reg emplAft6M_0M_In p_emplAft6M_0M_In
	
	local R_sq = e(r2)
	local beta = _b[p_emplAft6M_0M_In]
	
	* Calculate the covariance between the empirical JFR and the predicted JFR
	correlate emplAft6M_0M_In p_emplAft6M_0M_In, covariance
	local cov = r(cov_12)		
	
	 * Save to frame
	frame post sum ("`model'") (`R_sq')	(`beta') (`cov')
	
}


/*******************************************************************************
*	Main text table: sequential vs marginal contributions
********************************************************************************/

* Generate table with variable groups:
frame change sum
keep vars R_sq
sort vars

gen base = 1 if vars == "Full_SeqDrop_incIndiv"
replace base = 2 if strpos(vars, "_Ba_") >= 1
replace base = 3 if vars == "Full"
replace base = 4 if strpos(vars, "_Ba_") < 1 & vars != "Full" & vars != "Full_SeqDrop_incIndiv"

replace vars = subinstr(vars, "EX_FullSample_", "", .)
replace vars = subinstr(vars, "EX_Ba_FullSample_", "", .)
replace vars = "Base" if inlist(vars, "Full_SeqDrop_incIndiv", "Full")


* Transform to wide format:
reshape wide R_sq, i(vars) j(base)

egen R_sq1_temp = mean(R_sq1)
egen R_sq3_temp = mean(R_sq3)

replace R_sq1 = R_sq1_temp
replace R_sq3 = R_sq3_temp

drop R_sq1_temp R_sq3_temp
drop if vars == "Base"


* Add percentage increases:
gen incr1_temp = 100 * (R_sq2 / R_sq1 - 1)
gen incr2_temp = 100 * (R_sq4 / R_sq3 - 1)

* Format them properly:
gen incr1 = "+" + string(incr1_temp, "%9.1f") + "\%" if incr1_temp>0
replace incr1 = string(incr1_temp, "%9.1f") + "\%" if incr1_temp<=0
gen incr2 = "+" + string(incr2_temp, "%9.1f") + "\%" if incr2_temp>0
replace incr2 = string(incr2_temp, "%9.1f") + "\%" if incr2_temp<=0

drop incr1_temp incr2_temp

* Re-order variables:
order vars R_sq1 R_sq2 incr1 R_sq3 R_sq4 incr2

gen ord = .
replace ord = 4 if vars == "IQ"
replace ord = 1 if vars == "OC"
replace ord = 6 if vars == "RR"
replace ord = 5 if vars == "UI"
replace ord = 2 if vars == "UM"
replace ord = 3 if vars == "WE"
replace ord = 7 if vars == "ALL"


* Rename rows:
replace vars = "IQ" if vars == "IQ"
replace vars = "Occupation" if vars == "OC"
replace vars = "UI benefits" if vars == "RR"
replace vars = "UI choice" if vars == "UI"
replace vars = "Union member" if vars == "UM"
replace vars = "Wages" if vars == "WA"
replace vars = "Wealth" if vars == "WE"
replace vars = "All" if vars == "ALL"

sort ord

* Add to table 3:
file open myfile using "${output}/111_1_Explanatory_power_of_variables_Submodels_table.tex", write append

file write myfile "%\documentclass{article}" _newline ///
	"%\usepackage{booktabs}" _newline ///
	"%\usepackage[margin=1in]{geometry}" _newline ///
	"%\begin{document}" _newline ///
	"%\begin{table}[h] \centering" _newline ///
	"%\footnotesize \begin{tabular}{l c c c c c c c c}" _newline ///
	"%\hline \hline \addlinespace[3ex]" _newline ///
	///
	"& \multicolumn{8}{c}{\large \textbf{C. Extensions of Baseline}} \\ \addlinespace[3ex]" _newline ///
	" & \normalsize (1) & \normalsize (2) & \normalsize (3) & \normalsize (4) & \normalsize (5) & \normalsize (6) & \normalsize (7) & \normalsize (8) \\ \addlinespace[1.5ex] " _newline ///
	"\cline{2-9}  \addlinespace[1.5ex]" _newline ///
	"\normalsize \(R^2(\hat{F}_{0}, F_{0})\) & \normalsize `:di %9.3f `= R_sq3[1]'' & \normalsize `:di %9.3f `=R_sq4[1]'' & \normalsize `:di %9.3f `=R_sq4[2]'' & \normalsize `:di %9.3f `=R_sq4[3]'' & \normalsize `:di %9.3f `=R_sq4[4]'' & \normalsize `:di %9.3f `=R_sq4[5]'' & \normalsize `:di %9.3f `=R_sq4[6]'' & \normalsize `:di %9.3f `=R_sq4[7]'' \\ \addlinespace[0.2cm]" _newline ///
	"Change (\(j\)) vs (1) & - & `= incr2[1]' & `= incr2[2]' & `= incr2[3]' & `= incr2[4]' & `= incr2[5]' & `= incr2[6]' & `= incr2[7]' \\ \addlinespace[3ex]" _newline ///
	" \cline{2-9} \addlinespace[1.5ex]" _newline ///
	"\small Baseline Variables & X & X & X & X & X & X & X & X \\ \addlinespace[0cm]" _newline ///
	"\small `=vars[1]' & & X & & & & & & X \\ \addlinespace[0cm]" _newline ///
	"\small `=vars[2]' & & & X & & & & & X \\ \addlinespace[0cm]" _newline ///
	"\small `=vars[3]' & & & & X & & & & X \\ \addlinespace[0cm]" _newline ///
	"\small `=vars[4]' & & & & & X & & & X \\ \addlinespace[0cm]" _newline ///
	"\small `=vars[5]' & & & & & & X & & X \\ \addlinespace[0cm]" _newline ///
	"\small `=vars[6]' & & & & & & & X & X \\ \addlinespace[0cm]" _newline ///
	"\addlinespace[3ex]" _newline ///
	///
	"\hline \hline \addlinespace[1.5ex]" _newline ///
	"\end{tabular}" _newline ///
	"%\end{table}" _newline ///
	"%\end{document}"
file close myfile	

* Appendix table for Basic + Extensions:
file open myfile using "${output}/${id_code}_Explanatory_power_of_variables_BasicExpanded_table.tex", write replace

file write myfile "%\documentclass{article}" _newline ///
	"%\usepackage{booktabs}" _newline ///
	"%\usepackage[margin=1in]{geometry}" _newline ///
	"%\begin{document}" _newline ///
	"%\begin{table}[h] \centering" _newline ///
	"\footnotesize \begin{tabular}{l c c c c c c c c}" _newline ///
	"\hline \hline \addlinespace[3ex]" _newline ///
	///
	"& \multicolumn{8}{c}{\large \textbf{Extensions of Basic}} \\ \addlinespace[3ex]" _newline ///
	" & \normalsize (1) & \normalsize (2) & \normalsize (3) & \normalsize (4) & \normalsize (5) & \normalsize (6) & \normalsize (7) & \normalsize (8) \\ \addlinespace[1.5ex] " _newline ///
	"\cline{2-9}  \addlinespace[1.5ex]" _newline ///
	"\normalsize \(R^2(\hat{F}_{0}, F_{0})\) & \normalsize `:di %9.3f `= R_sq1[1]'' & \normalsize `:di %9.3f `=R_sq2[1]'' & \normalsize `:di %9.3f `=R_sq2[2]'' & \normalsize `:di %9.3f `=R_sq2[3]'' & \normalsize `:di %9.3f `=R_sq2[4]'' & \normalsize `:di %9.3f `=R_sq2[5]'' & \normalsize `:di %9.3f `=R_sq2[6]'' & \normalsize `:di %9.3f `=R_sq2[7]'' \\ \addlinespace[0.2cm]" _newline ///
	"Change (\(j\)) vs (1) & - & `= incr1[1]' & `= incr1[2]' & `= incr1[3]' & `= incr1[4]' & `= incr1[5]' & `= incr1[6]' & `= incr1[7]' \\ \addlinespace[3ex]" _newline ///
	" \cline{2-9} \addlinespace[1.5ex]" _newline ///
	"\small Socio-demographics & X & X & X & X & X & X & X & X \\ \addlinespace[0cm]" _newline ///
	"\small `=vars[1]' & & X & & & & & & X  \\ \addlinespace[0cm]" _newline ///
	"\small `=vars[2]' & & & X & & & & & X \\ \addlinespace[0cm]" _newline ///
	"\small `=vars[3]' & & & & X & & & & X \\ \addlinespace[0cm]" _newline ///
	"\small `=vars[4]' & & & & & X & & & X \\ \addlinespace[0cm]" _newline ///
	"\small `=vars[5]' & & & & & & X & & X \\ \addlinespace[0cm]" _newline ///
	"\small `=vars[6]' & & & & & & & X & X \\ \addlinespace[0cm]" _newline ///
	"\addlinespace[3ex]" _newline ///
	///
	"\hline \hline \addlinespace[1.5ex]" _newline ///
	"\end{tabular}" _newline ///
	"%\end{table}" _newline ///
	"%\end{document}"
file close myfile	
